<?php
class Product extends AppModel {
	var $name = 'Product';
    var $validate = array(
        'code' => array(
            'isUnique' => array(
                'rule'       => 'isUnique',
                'allowEmpty' => false,
                'on'         => 'create',
                'message'    => 'This product code has already existed in database'
            )
        ),
        'name' => array(
            'notEmpty' => array(
                'rule'    => 'notEmpty',
                'message' => 'This field cannot be left blank'
            )
        ),
        'bv' => array(
            'numeric' => array(
                'rule'       => 'numeric',
                'allowEmpty' => false,
                'message'    => 'This field allow numeric only'
            )
        ),
        'cost' => array(
            'money' => array(
                'rule'       => 'money',
                'allowEmpty' => true,
                'message'    => 'Please supply a valid monetary amount'
            )
        )
    );


    public function listing ($params) {
        $page = $params['page'];
        $cond = 'p.`status`!="D" ';

        if ($params['query'] && $params['qtype']) {
            $cond .= 'AND p.`'.$params['qtype'].'` LIKE "%'.$params['query'].'%" ';
        }

        $sql = 'SELECT COUNT(*) AS total FROM `products` AS p ' .
               'WHERE '.$cond;
        $ttl = $this->query($sql);

        $total = (count($ttl) == 0) ? 0 : $ttl[0][0]['total'];
        if ($total == 0) {
            return 0;
        }

        $rpp   = isset($params['rp']) ? $params['rp'] : 10;
        $start = (($page-1) * $rpp);
        $sort  = empty($params['sortname']) ? 'id' : $params['sortname'];
        $ord   = (empty($params['sortorder']) || $params['sortorder'] == 'asc') ? 'ASC' : 'DESC';
        $sql   = 'SELECT p.* FROM `products` AS p ' .
                 'WHERE '.$cond .
                 'ORDER BY p.`'.$sort.'` '.$ord.' ' .
                 'LIMIT '.$start.', '.$rpp;
        $data = $this->query($sql);
        $rec  = array();

        if (count($data) == 0) {
            return 0;
        }

        foreach ($data as $rs) {
            $id = $rs['p']['id'];

            $actions  = '<div class="editRecord" onclick="editRecord('.$id.');"></div>';
            $created  = $this->serverToClientTime($rs['p']['created'], 'd/m/Y H:i a');
            $modified = $this->serverToClientTime($rs['p']['modified'], 'd/m/Y H:i a');

            $cell = array(
                $rs['p']['id'], $rs['p']['code'], $rs['p']['name'], $rs['p']['description'], $rs['p']['bv'],
                $rs['p']['cost'], $rs['p']['price'], $rs['p']['include_ids'], $created, $modified, $actions
            );

            $rec[] = array(
                'id'   => $id,
                'cell' => $cell
            );
        }

        return array(
            'page'  => $page,
            'total' => $total,
            'rows'  => $rec
        );
    } // listing

    public function getInfoByCode ($code) {
        if (substr($code, -2) == ', ') {
            $code = substr($code, 0, -2);
        }

        $codes = @explode(', ', $code);

        if (!is_array($codes)) {
            return false;
        }

        $resp = array();
        $data = $this->find('all', array(
            'conditions' => array('code' => $codes),
            'recursive'  => -1,
            'fields'     => 'id, code, cost, price',
            'order'      => 'id ASC'
        ));

        if ($data && count($data) == 0) {
            return false;
        }

        foreach ($data as $p) {
            $resp[] = array(
                'id'    => $p['Product']['id'],
                'code'  => $p['Product']['code'],
                'cost'  => $p['Product']['cost'],
                'price' => $p['Product']['price']
            );
        }

        return $resp;
    } // getInfoByCode
} // Product
?>